﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Reflection;

namespace LiJu.Db
{
     public class DbExtension
    {
        public static List<T> DataTableToList<T>(DataTable dataTable) where T : new()
        {
            List<T> list = new List<T>();

            foreach (DataRow row in dataTable.Rows)
            {
                T obj = new T();
                foreach (DataColumn column in dataTable.Columns)
                {
                    PropertyInfo propertyInfo = typeof(T).GetProperty(column.ColumnName);
                    if (propertyInfo != null && row[column] != DBNull.Value)
                    {
                        propertyInfo.SetValue(obj, row[column]);
                    }
                }
                list.Add(obj);
            }

            return list;
        }
    }
}
